/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.wifibrowser.wifi;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2016年2月17日 上午10:52:43
 * @Title <B>todo</B>
 *        <pre>
 * todo
 * </pre>
 */

public class WifiPwdManager {
	
	public String findPwdBySql(String ssidOnline){
		
		return "";
	}
	

	/**
	 * 
	 * <pre>
	 * 终端：adb shell 
	 * 1、adb connect 192.168.1.6。这是使用adb来连接手机。如果adb没有配置到环境变量中，请先定位到adb所在的目录，然后执行该命令或者将其所在目录加入环境变量中。
	 * 2、adb shell。这是启动手机的命令终端。
	 * 3、cat /data/misc/wifi/*.conf。获取/data/misc/wifi文件夹下所有conf文件后缀的信息
	 * java 代码中：
	 * 1、通过Runtime.getRuntime().exec("su")获取root权限。
	 * 2、通过process.getOutputStream()和process.getInputStream()获取终端的输入流和输出流。
	 * 3、通过dataOutputStream.writeBytes("cat /data/misc/wifi/*.conf\n")往终端中输入命令。注意，这里必须要有\n作为换行，否则会与后一个exit命令作为一个命令，最终导致命令执行失败，无法得到结果。
	 * 4、通过dataInputStream获取命令执行结果，并以UTF-8的编码转换成字符串。
	 * 5、使用正则表达式过滤出wifi的用户名和密码。
	 * </pre>
	 * 
	 * @return
	 * @throws Exception
	 * @return List<WifiInfo>
	 */
	public String findPwdByWpaSupplicantConf(String ssidOnline) throws Exception {
		if(ssidOnline==null || "".equals(ssidOnline))return null;
		Process process = null;
		DataOutputStream dataOutputStream = null;
		DataInputStream dataInputStream = null;
		StringBuffer wifiConf = new StringBuffer();
		try {
			process = Runtime.getRuntime().exec("su");
			dataOutputStream = new DataOutputStream(process.getOutputStream());
			dataInputStream = new DataInputStream(process.getInputStream());
			dataOutputStream.writeBytes("cat /data/misc/wifi/*.conf\n");
			dataOutputStream.writeBytes("exit\n");
			dataOutputStream.flush();
			InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				wifiConf.append(line);
			}
			bufferedReader.close();
			inputStreamReader.close();
			process.waitFor();
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (dataOutputStream != null) {
					dataOutputStream.close();
				}
				if (dataInputStream != null) {
					dataInputStream.close();
				}
				process.destroy();
			} catch (Exception e) {
				throw e;
			}
		}
		String ssid=null,pwd=null;
		Pattern network = Pattern.compile("network=\\{([^\\}]+)\\}", Pattern.DOTALL);
		Matcher networkMatcher = network.matcher(wifiConf.toString());
		while (networkMatcher.find()) {
			String networkBlock = networkMatcher.group();
			Pattern pattern = Pattern.compile("ssid=\"([^\"]+)\"");
			Matcher matcher = pattern.matcher(networkBlock);
			if (matcher.find()) {
				ssid = matcher.group(1);
				if(ssidOnline.equals(ssid)){
					pattern = Pattern.compile("psk=\"([^\"]+)\"");
					matcher = pattern.matcher(networkBlock);
					if (matcher.find()) {
						pwd = matcher.group(1);
						break;
					} 
				}
			}
		}
		return pwd;
	}
}
